import 'package:flutter/cupertino.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_demo/bean/home_module.dart';
import 'package:flutter_demo/pages/animation/animation_page.dart';
import 'package:flutter_demo/pages/base/badge_page.dart';
import 'package:flutter_demo/pages/base/button_page.dart';
import 'package:flutter_demo/pages/base/checkbox_page.dart';
import 'package:flutter_demo/pages/base/divider_page.dart';
import 'package:flutter_demo/pages/base/event/listener_page.dart';
import 'package:flutter_demo/pages/base/image_page.dart';
import 'package:flutter_demo/pages/base/indicator_page.dart';
import 'package:flutter_demo/pages/base/layout_params/align_page.dart';
import 'package:flutter_demo/pages/base/layout_params/container_page.dart';
import 'package:flutter_demo/pages/base/layout_params/physical_model_page.dart';
import 'package:flutter_demo/pages/base/layout_params/positioned_page.dart';
import 'package:flutter_demo/pages/base/text_page.dart';
import 'package:flutter_demo/pages/compatible/flutter_channel_page.dart';
import 'package:flutter_demo/pages/container/flex_page.dart';
import 'package:flutter_demo/pages/container/flow_page.dart';

import '../../pages/advanced/app_page.dart';
import '../../pages/advanced/dialog_page.dart';
import '../../pages/advanced/filter_page.dart';
import '../../pages/advanced/scaffold_page.dart';
import '../../pages/advanced/shader_mask_page.dart';
import '../../pages/advanced/sliver_page.dart';
import '../../pages/advanced/segmented_button_page.dart';
import '../../pages/advanced/tab_bar_page.dart';
import '../../pages/animation/animated_widget_page.dart';
import '../../pages/animation/content_switch_animation_page.dart';
import '../../pages/animation/implicitly_animated_widget_page.dart';
import '../../pages/animation/list_item_animation_page.dart';
import '../../pages/base/edit_text_page.dart';
import '../../pages/base/event/gesture_detector_page.dart';
import '../../pages/base/layout_params/clip_page.dart';
import '../../pages/base/layout_params/decoration_page.dart';
import '../../pages/base/layout_params/opacity_page.dart';
import '../../pages/base/layout_params/padding_page.dart';
import '../../pages/base/layout_params/size_page.dart';
import '../../pages/base/base/stateful_widget_page.dart';
import '../../pages/base/base/stateless_widget.dart';
import '../../pages/base/layout_params/transform_page.dart';
import '../../pages/base/base/widget.dart';
import '../../pages/base/radio_page.dart';
import '../../pages/base/slider_page.dart';
import '../../pages/base/switch_page.dart';
import '../../pages/container/fitted_box_page.dart';
import '../../pages/container/intrinsic_page.dart';
import '../../pages/container/list_page.dart';
import '../../pages/container/overflow_box_page.dart';
import '../../pages/container/page_view_page.dart';
import '../../pages/container/scroll_view_page.dart';
import '../../pages/container/stack_page.dart';
import '../../pages/container/table_page.dart';
import '../../pages/container/wrap_page.dart';
import '../../pages/custom/custom_layout_page.dart';
import '../../pages/custom/custom_paint_page.dart';
import '../../pages/framework/async_page.dart';
import '../../pages/framework/lifecycle_listener_page.dart';
import '../../pages/framework/media_query_page.dart';
import '../../pages/framework/navigation_page.dart';
import '../../pages/third/bloc_page.dart';

///首页的模块列表状态
@immutable
abstract class HomeModulesState {
  /// 首页模块列表数据
  final List<ModuleInfo> moduleList;

  /// 当前模块列表选中的序列
  final int selectedIndex;

  const HomeModulesState(this.moduleList, this.selectedIndex);

  /// 命名的第二构造函数，从其它状态构造新实例，并更新部分属性
  /// - [state] 其它状态，一般是指之前的状态
  HomeModulesState.of(
    HomeModulesState state, {
    List<ModuleInfo>? moduleList,
    int? selectedIndex,
  }) : this(
          moduleList ?? state.moduleList,
          selectedIndex ?? state.selectedIndex,
        );

  /// 获取当前选中共的模块
  ModuleInfo? getSelectedModuleInfo() {
    if (moduleList.isNotEmpty &&
        selectedIndex >= 0 &&
        selectedIndex < moduleList.length) {
      return moduleList[selectedIndex];
    } else {
      return null;
    }
  }
}

///初始化
class OnInit extends HomeModulesState {
  OnInit() : super([], 0);
}

/// 模块列表发生改变
class OnModuleListUpdate extends HomeModulesState {
  OnModuleListUpdate(super.state, List<ModuleInfo> moduleList)
      : super.of(moduleList: moduleList);
}

/// 当前选中模块发生改变
class OnSelectedIndexUpdate extends HomeModulesState {
  OnSelectedIndexUpdate(super.state, int selectedIndex)
      : super.of(selectedIndex: selectedIndex);
}

/// 首页模块业务逻辑
class HomeModulesCubit extends Cubit<HomeModulesState> {
  HomeModulesCubit(BuildContext context) : super(OnInit()) {
    _initModuleList(context);
  }

  /// 初始化模块列表
  void _initModuleList(BuildContext context) {
    updateModuleList([
      ModuleInfo("基础组件", [
        "基础",
        PageInfo(const WidgetPage()),
        PageInfo(const StatelessWidgetPage()),
        PageInfo(const StatefulWidgetPage()),
        "布局参数",
        PageInfo(const PositionedPage()),
        PageInfo(const AlignPage()),
        PageInfo(const PaddingPage()),
        PageInfo(const SizePage()),
        PageInfo(const DecorationPage()),
        PageInfo(const PhysicalModelPage()),
        PageInfo(const TransformPage()),
        PageInfo(const ContainerPage()),
        PageInfo(const OpacityPage()),
        PageInfo(const ClipPage()),
        "事件",
        PageInfo(ListenerPage()),
        PageInfo(GestureDetectorPage()),
        "视图",
        PageInfo(const TextPage()),
        PageInfo(const EditTextPage()),
        PageInfo(const ImagePage()),
        PageInfo(const ButtonPage()),
        PageInfo(const IndicatorPage()),
        PageInfo(const SliderPage()),
        PageInfo(const SwitchPage()),
        PageInfo(const RadioPage()),
        PageInfo(const CheckboxPage()),
        PageInfo(const DividerPage()),
        PageInfo(const BadgePage()),

      ]),
      ModuleInfo("容器组件", [
        PageInfo(const StackPage()),
        PageInfo(const FlexPage()),
        PageInfo(const ScrollViewPage()),
        PageInfo(const ListPage()),
        PageInfo(const PageViewPage()),
        PageInfo(const WrapPage()),
        PageInfo(const FlowPage()),
        PageInfo(const FittedBoxPage()),
        PageInfo(const OverflowBoxPage()),
        PageInfo(const IntrinsicPage()),
        PageInfo(const TablePage()),
      ]),
      ModuleInfo("自定义组件", [
        PageInfo(const CustomPaintPage()),
        PageInfo(const CustomLayoutPage()),
      ]),
      ModuleInfo("动画", [
        PageInfo(const AnimationPage()),
        PageInfo(const AnimatedWidgetPage()),
        PageInfo(const ImplicitlyAnimatedWidgetPage()),
        PageInfo(const ContentSwitchAnimationPage()),
        PageInfo(const ListItemAnimationPage()),
      ]),
      ModuleInfo("高级组件", [
        PageInfo(const AppPage()),
        PageInfo(const ScaffoldPage()),
        PageInfo(const DialogPage()),
        PageInfo(const TabBarPage()),
        PageInfo(const SegmentedButtonPage()),
        PageInfo(const ShaderMaskPage()),
        PageInfo(const FilterPage()),
        PageInfo(const SliverPage()),
      ]),
      ModuleInfo("框架API", [
        PageInfo(const NavigationPage()),
        PageInfo(const MediaQueryPage()),
        PageInfo(const LifecycleListenerPage()),
        PageInfo(const AsyncPage()),
      ]),
      ModuleInfo("兼容性", [
        PageInfo(const FlutterChannelPage()),
      ]),
      ModuleInfo("第三方包", [
        PageInfo(const BlocPage()),
      ]),
    ]);
  }

  /// 更新模块列表
  void updateModuleList(List<ModuleInfo> moduleList) {
    emit(OnModuleListUpdate(state, moduleList));
  }

  /// 更新已选中的模块下标
  void updateSelectedIndex(int index) {
    emit(OnSelectedIndexUpdate(state, index));
  }
}
